package com.roads.project.service.impl;

import java.util.List;

import com.roads.common.constant.Constants;
import com.roads.common.constant.UserConstants;
import com.roads.common.utils.DateUtils;
import com.roads.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.roads.project.mapper.ProMaterialBoqMapper;
import com.roads.project.domain.ProMaterialBoq;
import com.roads.project.service.IProMaterialBoqService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 材料清单Service业务层处理
 * 
 * @author nhp
 * @date 2022-01-02
 */
@Service
public class ProMaterialBoqServiceImpl implements IProMaterialBoqService {

    @Autowired
    private ProMaterialBoqMapper mapper;

    /**
     * 根据主键ID查询详情
     * 
     * @param id 材料清单主键
     * @return 材料清单
     */
    @Override
    public ProMaterialBoq getModelById(Long id) {
        return mapper.getModelById(id);
    }

    /**
     * 查询列表
     * 
     * @param model 材料清单
     * @return 材料清单
     */
    @Override
    public List<ProMaterialBoq> selectModelList(ProMaterialBoq model) {
        return mapper.selectModelList(model);
    }

    /**
     * 查询材料清单树形结构(名称=材料编号+材料名称)
     *
     * @return 材料清单集合
     */
    @Override
    public List<ProMaterialBoq> selectTreeListFormatName() {
        return mapper.selectTreeListFormatName();
    }

    /**
     * 校验材料编号是否唯一
     *
     * @param model 材料清单实体
     * @return 结果
     */
    @Override
    public String checkBoqCodeUnique(ProMaterialBoq model) {
        Long id = StringUtils.isNull(model.getBoqId()) ? -1L : model.getBoqId();
        ProMaterialBoq search = mapper.getModelByCode(model.getBoqCode());
        if (StringUtils.isNotNull(search) && !search.getBoqId().equals(id)) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 查询材料清单下子清单数量
     *
     * @param boqCode 材料清单编号
     * @return 数量
     */
    @Override
    public int getCountChildren(String boqCode) {
        return mapper.getCountChildren(boqCode);
    }

    /**
     * 新增
     * 
     * @param model 材料清单
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(ProMaterialBoq model) {
        model.setCreateTime(DateUtils.getNowDate());
        // 新增材料清单默认都是叶子节点
        model.setLeafNode(Constants.YES);
        String parentCode = model.getParentCode();
        if (StringUtils.isEmpty(parentCode)) {
            model.setLevel(1);
            model.setAncestors(model.getBoqCode());
        } else {
            ProMaterialBoq parent = mapper.getModelByCode(parentCode);
            // 更新父项是否叶节点为NO
            if (Constants.YES.equals(parent.getLeafNode())) {
                parent.setLeafNode(Constants.NO);
                mapper.update(parent);
            }
            model.setLevel(parent.getLevel() + 1);
            model.setAncestors(parent.getAncestors() + "," + model.getBoqCode());
        }
        return mapper.insert(model);
    }

    /**
     * 修改
     * 
     * @param model 材料清单
     * @return 结果
     */
    @Override
    public int update(ProMaterialBoq model) {
        model.setUpdateTime(DateUtils.getNowDate());
        return mapper.update(model);
    }

    /**
     * 批量删除
     * 
     * @param ids 主键集合
     * @return 结果
     */
    @Override
    public int deleteByIds(Long[] ids) {
        return mapper.deleteByIds(ids);
    }

    /**
     * 删除
     * 
     * @param id 材料清单主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int delete(ProMaterialBoq model, Long id) {
        mapper.delete(id);
        // 处理父项清单
        if (StringUtils.isNotEmpty(model.getParentCode())) {
            int childrenCount = mapper.getCountChildren(model.getParentCode());
            if (childrenCount == 0) {
                ProMaterialBoq parent = mapper.getModelByCode(model.getParentCode());
                parent.setLeafNode(Constants.YES);
                mapper.update(parent);
            }
        }
        return 1;
    }
}
